package entities.forum;

import entities.User;
import entities.reports.ReportedMessage;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * Trabalho PD - 2014/2015
 *
 * @author a21210380 a21210392
 */
@Entity
@Table(name = "message")
@NamedQueries({
    @NamedQuery(name = "Message.findAll", query = "SELECT m FROM Message m"),
    @NamedQuery(name = "Message.findByIdMessage", query = "SELECT m FROM Message m WHERE m.idMessage = :idMessage"),
    @NamedQuery(name = "Message.findByTopic", query = "SELECT m FROM Message m WHERE m.topic = :topic"),
    @NamedQuery(name = "Message.findAllByReceivingUser", query = "SELECT m FROM Message m WHERE m.idReceivedUser.idUser = :idUser"),
    @NamedQuery(name = "Message.findByReceivingUser", query = "SELECT m FROM Message m WHERE m.idReceivedUser.idUser = :idUser AND m.idMessage = :idMessage"),
    @NamedQuery(name = "Message.findByMessage", query = "SELECT m FROM Message m WHERE m.message = :message")})
public class Message implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idMessage")
    private Integer idMessage;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "topic")
    private String topic;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 700)
    @Column(name = "message")
    private String message;

    @Basic(optional = false)
    @NotNull
    @Column(name = "sentDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date sentDate;

    @JoinColumn(name = "idReceivedUser", referencedColumnName = "idUser")
    @ManyToOne(optional = false)
    private User idReceivedUser;

    @JoinColumn(name = "idSentUser", referencedColumnName = "idUser")
    @ManyToOne(optional = false)
    private User idSentUser;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idMessage")
    private Collection<ReportedMessage> reports;

    public Message() {
    }

    public Message(User idReceivedUser, User idSentUser, String topic, String message, Date sentDate) {
        this.idReceivedUser = idReceivedUser;
        this.idSentUser = idSentUser;
        this.topic = topic;
        this.message = message;
        this.sentDate = sentDate;
        this.reports = new ArrayList<>();
    }

    public Integer getId() {
        return idMessage;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }
    
    public String getMessage() {
        return message;
    }
    
    public Date getSentDate() {
        return sentDate;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public User getReceivingUser() {
        return idReceivedUser;
    }

    public User getSentUser() {
        return idSentUser;
    }

    public Collection<ReportedMessage> getReports() {
        return reports;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idMessage != null ? idMessage.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Message)) {
            return false;
        }
        Message other = (Message) object;
        return !((this.idMessage == null && other.idMessage != null) || (this.idMessage != null && !this.idMessage.equals(other.idMessage)));
    }

    @Override
    public String toString() {
        return "entities.extra.Message[ idMessage=" + idMessage + " ]";
    }

}
